package cn.haohaowo.service.impl;

import cn.haohaowo.common.Constants;
import cn.haohaowo.common.Page;
import cn.haohaowo.entity.Book;
import cn.haohaowo.service.BookBizService;
import cn.haohaowo.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class BookBizServiceImpl implements BookBizService {

	@Autowired(required = false)
	private RestTemplate restTemplate;

	public Book getBook(int bookId) {

		String url = Constants.RESTFUL_URL + "book/queryBookById";
		MultiValueMap<String, Object> para = new LinkedMultiValueMap<>();
		para.set("bookId", bookId);
		String result = restTemplate.postForObject(url, para, String.class);
		if (StringUtils.isNotEmpty(result)) {
			Book book = JSONObject.parseObject(result, Book.class);
			return book;
		}

		return null;

	}

	public Page<Book> getBooks(String type, String title, String author,
			String publisher, String minPrice, String maxPrice, int pageNo) {

		return getBooks(type, title, author, publisher, minPrice,maxPrice, pageNo, Constants.PAGE_SIZE);

	}

	public Page<Book> getBooks(String type, String title, String author,
			String publisher, String minPrice, String maxPrice, int pageNo,
			int pageSize) {

		String url = Constants.RESTFUL_URL + "book/queryBooks";
		MultiValueMap<String, Object> para = new LinkedMultiValueMap<>();
		para.set("type", type);
		para.set("title", title);
		para.set("author", author);
		para.set("publisher", publisher);
		para.set("minPrice", minPrice);
		para.set("maxPrice", maxPrice);
		para.set("pageNo", pageNo);
		para.set("pageSize", pageSize);
		String result = restTemplate.postForObject(url, para, String.class);
		if (StringUtils.isNotEmpty(result)) {
			Page<Book> bookPage = JSONObject.parseObject(result, Page.class);
			return bookPage;
		}

		return null;

	}

	public List<Book> getHotBooks() {

		String url = Constants.RESTFUL_URL + "book/queryHotBooks";
		MultiValueMap<String, Object> para = new LinkedMultiValueMap<>();
		String result = restTemplate.postForObject(url, para, String.class);
		if (StringUtils.isNotEmpty(result)) {
			List<Book> bookList = JSONObject.parseArray(result, Book.class);
			return bookList;
		}

		return null;

	}

	public List<Book> getRelativeBooks(int bookId) {

		String url = Constants.RESTFUL_URL + "book/queryRelativeBooks";
		MultiValueMap<String, Object> para = new LinkedMultiValueMap<>();
		para.set("bookId", bookId);
		String result = restTemplate.postForObject(url, para, String.class);
		if (StringUtils.isNotEmpty(result)) {
			List<Book> bookList = JSONObject.parseArray(result, Book.class);
			return bookList;
		}

		return null;

	}

	public Page<Book> search(String keyword, int pageNo) {
		return search(keyword, pageNo, Constants.PAGE_SIZE);
	}

	public Page<Book> search(String keyword, int pageNo, int pageSize) {

		String url = Constants.RESTFUL_URL + "book/queryBooks";
		MultiValueMap<String, Object> para = new LinkedMultiValueMap<>();
		para.set("keyword", keyword);
		para.set("pageNo", pageNo);
		para.set("pageSize", pageSize);
		String result = restTemplate.postForObject(url, para, String.class);
		if (StringUtils.isNotEmpty(result)) {
			Page<Book> bookPage = JSONObject.parseObject(result, Page.class);
			return bookPage;
		}

		return null;

	}

}
